package com.longteng.diamond.controller;

import com.longteng.diamond.domain.RunTestContext;
import com.longteng.diamond.domain.TestCase;
import com.longteng.diamond.po.RequestResult;
import com.longteng.diamond.service.MailReportService;
import com.longteng.diamond.service.RunTestService;
import com.longteng.diamond.thread.ThreadRunTest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import java.util.concurrent.Executor;

/**
 * Created by Administrator
 * DATE : 2018/7/14
 * TIME : 17:39
 * PROJECT : java-lesson
 * PACKAGE : com.longteng.lesson
 *
 * @author <a href="mailto:taki@qq.com">Administrator</a>
 */
@RestController
@Slf4j
@RequestMapping("/runTestController")
public class RunTestController {

    @Autowired
    RunTestService runTestService;
    @Autowired
    MailReportService mailReportService;
    @Autowired
    Executor executor;
    /**
     * 通过构建计划执行
     * @param buildTestId
     * @return requestResult
     * */
    @RequestMapping("/runTestByBuildId")
    public
    ModelAndView runTestByBuildId(Integer buildTestId) {
       ModelAndView modelAndView=new ModelAndView();
        try {
            /**多线程，异步执行**/
            ThreadRunTest threadRunTest=new ThreadRunTest(buildTestId,runTestService,mailReportService);
            executor.execute(threadRunTest);
        } catch (Exception e) {
            log.error("调用异常!", e);
        }
        modelAndView.setViewName("redirect:/testPlanController/planPage?buildTestId="+buildTestId);
        return modelAndView;
    }
    /**
     * 单个用例调用
     * @param testCase
     * @return requestResult
     * */
    @RequestMapping("/runTestCallByCase")
    public RequestResult runTestCallByCase(TestCase testCase) {
        RequestResult requestResult = new RequestResult();
        try {
            requestResult = runTestService.runTest(testCase);
        } catch (Exception e) {
            requestResult.setSuccess(false);
            requestResult.setMsg("调用异常,请联系管理员,异常信息"+e.toString());
            log.error("调用异常!", e);
        }
        return requestResult;
    }
}
